﻿$(document).ready(function () {
    plupload.addI18n({
        'Select files': 'בחר קבצים',
        'Add files to the upload queue and click the start button.': 'הוסף קבצים ולחץ על הכפתור "התחל העלאה"',
        'Filename': 'שם קובץ',
        'Status': 'סטטוס',
        'Size': 'גודל',
        'Add files': 'הוסף קבצים',
        'Start upload': 'התחל העלאה',
        'Stop current upload': 'עצור העלאה',
        'Start uploading queue': 'התחל את ההעלאה',
        'Drag files here.': 'גרור קבצים לכאן.',
        'Uploaded %d/%d files': '%d/%d קבצים הועלו',
        '%d files queued': '%d קבצים ממתינים'
    });

    function InitUploader() {
        var uploader = $("#uploader").pluploadQueue({
            // General settings
            runtimes: 'gears,flash,silverlight,browserplus,html5',
            url: pathPrefix + 'DesignVectors/UploadMultiple',
            max_file_size: '10mb',
            chunk_size: '1mb',
            unique_names: true,
            multipart: true,
            multipart_params: {},
            preinit: attachCallbacks,

            // Resize images on clientside if we can
            //resize: { width: 320, height: 240, quality: 90 },

            // Specify what files to browse for
            filters: [
			    {
			        title: "Flash files",
			        extensions: "swf"
			    }
		    ],

            // Flash settings
            flash_swf_url: pathPrefix + 'plupload/js/plupload.flash.swf',

            // Silverlight settings
            silverlight_xap_url: pathPrefix + 'plupload/js/plupload.silverlight.xap'
        });

        //before the upload start
        $("#uploader").pluploadQueue().bind('BeforeUpload', function (up, file) {
            $("#frmUploadImage :input").removeAttr("disabled");

            //append the form parameters to the request
            var data = $("#frmUploadImage :input:not(:checkbox)").serializeObject();
            $.extend(up.settings.multipart_params, data);

            $("#frmUploadImage :input").attr("disabled", "disabled");
        });
    }

    InitUploader();

    //plupload COMPLETE event
    function attachCallbacks(Uploader) {
        Uploader.bind('FileUploaded', function (up, file, Response) {
            //increase the FileIndex
            var fileIndex = parseInt($("#FileIndex").val());
            $("#FileIndex").val(fileIndex + 1);

            //check if all files have been uploaded
            if ((Uploader.total.uploaded + 1) == Uploader.files.length) {
                $("#frmUploadImage :input").removeAttr("disabled");

                window.setTimeout(function () {
                    InitUploader();
                }, 2000);
            }
        });
    }
});